# Maintainer: Alexey Pavlov <alexpux@gmail.com>
# Contributor: Martell Malone <martellmalone@gmail.com>
# Contributor: Ray Donnelly <mingw.android@gmail.com>

# toolchain build order: win32-api-headers->msys2-runtime->binutils->gcc->binutils->msys2-runtime
# NOTE: libtool requires rebuilt with each new gcc version

pkgbase=gcc
pkgname=('gcc' 'gcc-libs' 'gcc-fortran')
pkgver=4.9.2
pkgrel=6
pkgdesc="The GNU Compiler Collection"
arch=('i686' 'x86_64')
groups=('msys2-devel')
license=('GPL' 'LGPL' 'FDL' 'custom')
url="http://gcc.gnu.org"
makedepends=('binutils>=2.23' 'mpc-devel' 'cloog-devel' 'gmp-devel'
             'mpfr-devel' 'mpc-devel' 'isl-devel' 'zlib-devel'
             'windows-default-manifest')
STRIP_OPTION="strip"
#STRIP_OPTION="!strip"
#DEBUG_OPTION="debug"
DEBUG_OPTION="!debug"
options=('!emptydirs' "$STRIP_OPTION" "$DEBUG_OPTION")
source=(ftp://gcc.gnu.org/pub/gcc/releases/gcc-${pkgver}/gcc-${pkgver}.tar.bz2
        001-4.7-ada.patch
        005-4.7-boehm-gc-cygwin.patch
        010-4.7-execstack.patch
        015-4.7-java-awt.patch
        016-4.5-java-FIONREAD.patch
        017-4.7-java-jni.patch
        018-4.8-java-soname.patch
        025-4.8-libatomic-cygwin.patch
        030-4.7-libffi-noinst.patch
        031-4.8-libffi-cygwin64.patch
        035-4.7-libitm-cygwin.patch
        040-4.8-lto-plugin-soname.patch
        045-4.8-specs-cygwin.patch
        046-4.8-__cxa_atexit.patch
        047-4.8-libstdc-atexit_thread.patch
        048-4.8-medium-model-dllimport.patch
        050-4.5-skiptest.patch
        055-4.8-gcc-ada-DTR.patch
        060-4.8-Wimplicit-decls.patch
        950-4.8.0-configure-msys2.patch
        951-4.8.0-msys2-spec.patch
        952-4.8.0-libgcc-msys2.patch
        953-4.9.1-testsuite-msys2.patch
        955-4.9.2-apply-hack-so-gcc_s-isnt-stripped.patch
        1000-gcc49-isl-0.14-hack.patch)
md5sums=('4df8ee253b7f3863ad0b86359cd39c43'
         '0b96824eeebc3d444ce5447ec84fe482'
         '403c251e9794c9d183a6be03254c7bd5'
         '7eb73a1fec83520e9015553e03e48198'
         '1a0d50cfe02607d433d96baf3abb44e6'
         'f0b31458215043dcae74179cccab334c'
         '9a73b38c60bdc1341a1cf436194fb20f'
         'ad99cd62e7a91e334d238e14d8cc7267'
         'ed4bda44f75a1f0c69179724162ee74b'
         'a2888ff671f3d1c91e29dbc0382ab64a'
         'd9a6aa18c94f805ecf97f876eac95222'
         'ac437553f3d705955d5a51a1689803ab'
         'a3aae6965efe954e0eb07aab83765bbc'
         '1a73c015bc8431a79d7bba3480c665d7'
         '5a243bf768e2f0d733b5ebd993a29dbd'
         '59c007fedfc7bd6e8541b7ad339addfd'
         '66fb25ef1fd7f0412bdd7ad23d921060'
         '2f12113913da4fc3a992f5611009a506'
         'aed26d95b8a03d502b74811f88fff8bb'
         '577735db62e410b221a16f00d3010f4d'
         '82787563693b72ef96907179d11ae574'
         '6705926879c238f66b0430fae3ab4f37'
         '4092fcf2e81fbd292998633bef448cbe'
         'f2c3dc9f45f07d9da42289d117fed678'
         '983b08347388dfe506a09b8a317274fe'
         '944b730b936c2f44187e0638371d8e5f')

prepare() {
  cd ${srcdir}/gcc-${pkgver}

  # Remove files created by 001-4.7-ada.patch
  rm -f gcc/ada/a-intnam-cygwin.ads gcc/ada/mlib-tgt-specific-cygwin.adb \
        gcc/ada/s-gloloc-cygwin.adb gcc/ada/s-osinte-cygwin.ads \
        gcc/ada/s-taprop-cygwin.adb gcc/ada/system-cygwin.ads \
        gcc/ada/indepsw-cygwin.adb libgcc/config/i386/t-msys \
        gcc/config/i386/msys.h gcc/config/i386/msys-w64.h \
        gcc/config/i386/t-msys-w64 \
          > /dev/null 2>&1 | true

  # Remove file created by 045-4.8-specs-cygwin.patch
  rm -f  gcc/config/i386/cygwin.opt > /dev/null 2>&1 | true

  # Do not run fixincludes
  #sed -i 's@\./fixinc\.sh@-c true@' gcc/Makefile.in
  echo ${pkgver} > gcc/BASE-VER
  # hack! - some configure tests for header files using "$CPP $CPPFLAGS"
  sed -i "/ac_cpp=/s/\$CPPFLAGS/\$CPPFLAGS -O2/" {libiberty,gcc}/configure

  # Cygwin patches
  patch -p1 -i ${srcdir}/001-4.7-ada.patch
  patch -p1 -i ${srcdir}/005-4.7-boehm-gc-cygwin.patch
  patch -p1 -i ${srcdir}/010-4.7-execstack.patch
  patch -p1 -i ${srcdir}/015-4.7-java-awt.patch
  # http://gcc.gnu.org/bugzilla/show_bug.cgi?id=35536
  patch -p1 -i ${srcdir}/016-4.5-java-FIONREAD.patch
  patch -p1 -i ${srcdir}/017-4.7-java-jni.patch
  patch -p1 -i ${srcdir}/018-4.8-java-soname.patch
  patch -p1 -i ${srcdir}/025-4.8-libatomic-cygwin.patch
  patch -p1 -i ${srcdir}/030-4.7-libffi-noinst.patch
  patch -p1 -i ${srcdir}/031-4.8-libffi-cygwin64.patch
  patch -p1 -i ${srcdir}/035-4.7-libitm-cygwin.patch
  patch -p1 -i ${srcdir}/040-4.8-lto-plugin-soname.patch
  patch -p1 -i ${srcdir}/045-4.8-specs-cygwin.patch
  patch -p1 -i ${srcdir}/046-4.8-__cxa_atexit.patch
  patch -p1 -i ${srcdir}/047-4.8-libstdc-atexit_thread.patch
  patch -p1 -i ${srcdir}/048-4.8-medium-model-dllimport.patch
  patch -p1 -i ${srcdir}/050-4.5-skiptest.patch

  if [ "$CARCH" = "i686" ]
  then
    patch -p1 -i ${srcdir}/055-4.8-gcc-ada-DTR.patch
  fi

  patch -p1 -i ${srcdir}/060-4.8-Wimplicit-decls.patch

  # MSYS2 Patches
  patch -p1 -i ${srcdir}/950-4.8.0-configure-msys2.patch
  patch -p1 -i ${srcdir}/951-4.8.0-msys2-spec.patch
  patch -p1 -i ${srcdir}/952-4.8.0-libgcc-msys2.patch
  patch -p1 -i ${srcdir}/953-4.9.1-testsuite-msys2.patch
  patch -p1 -i ${srcdir}/955-4.9.2-apply-hack-so-gcc_s-isnt-stripped.patch

  # Compatibility with ISL>=0.14
  patch -p0 -i ${srcdir}/1000-gcc49-isl-0.14-hack.patch
  
  [ -d ${srcdir}/build-$(arch) ] && rm -rf ${srcdir}/build-$(arch)
  mkdir ${srcdir}/build-$(arch)
}

build() {
  cd ${srcdir}/build-$(arch)

  case ${CARCH} in
    i686)
      _arch=i686
      _arch_conf="--disable-sjlj-exceptions"
    ;;
    x86_64)
      _arch=x86-64
      _arch_conf=
    ;;
  esac

  # libgcc is built with -g0, but INHIBIT_LIBC_CFLAGS can
  # be used to undo that.
  if check_option "debug" "y"; then
    export INHIBIT_LIBC_CFLAGS="-g2"
  fi

  # using -pipe causes spurious test-suite failures
  # http://gcc.gnu.org/bugzilla/show_bug.cgi?id=48565
  CFLAGS=${CFLAGS/-pipe/}
  CXXFLAGS=${CXXFLAGS/-pipe/}

  ${srcdir}/gcc-${pkgver}/configure --build=${CHOST} \
    --prefix=/usr \
    --libexecdir=/usr/lib \
    --enable-bootstrap \
    --enable-shared --enable-shared-libgcc --enable-static \
    --enable-version-specific-runtime-libs \
    --with-arch=$_arch \
    --disable-multilib \
    $_arch_conf \
    --with-tune=generic \
    --enable-__cxa_atexit \
    --with-dwarf2 \
    --enable-languages=c,c++,fortran,lto \
    --enable-graphite \
    --enable-threads=posix \
    --enable-libatomic \
    --enable-libgomp \
    --disable-libitm \
    --enable-libquadmath \
    --enable-libquadmath-support \
    --enable-libssp \
    --disable-win32-registry \
    --disable-symvers \
    --with-gnu-ld \
    --with-gnu-as \
    --disable-isl-version-check \
    --enable-checking=release \
    --without-libiconv-prefix \
    --without-libintl-prefix \
    --with-system-zlib
  make

  # make documentation
  #cd $CHOST/libstdc++-v3
  #make doc-man-doxygen
}

# setup() {
#   cd ${srcdir}/build-$(arch)
#   make -j1 DESTDIR=${destdir} install
# }

check() {
  cd ${srcdir}/build-$(arch)

  # do not abort on error as some are "expected"
  make -k check || true
  ${srcdir}/gcc-${pkgver}/contrib/test_summary
}

package_gcc-libs()
{
  pkgdesc="Runtime libraries shipped by GCC"
  groups=('base')
  depends=('msys2-runtime')
  install=gcc-libs.install

  cd ${srcdir}/build-$(arch)
  make -j1 -C $CHOST/libgcc DESTDIR=${pkgdir} install-shared
  for lib in libgomp libstdc++-v3/src; do
    make -j1 -C $CHOST/$lib DESTDIR=${pkgdir} install-toolexeclibLTLIBRARIES
  done

  make -j1 -C $CHOST/libstdc++-v3/po DESTDIR=${pkgdir} install
  make -j1 -C $CHOST/libgomp DESTDIR=${pkgdir} install-info
  make -j1 -C $CHOST/libquadmath DESTDIR=${pkgdir} install-info

  make -j1 DESTDIR=${pkgdir} install-target-libquadmath
  make -j1 DESTDIR=${pkgdir} install-target-libgfortran
  make -j1 DESTDIR=${pkgdir} install-target-libssp
  make -j1 DESTDIR=${pkgdir} install-target-libatomic

  rm -f ${pkgdir}/*.dll
  rm -rf ${pkgdir}/usr/lib/

  # remove static libraries
  #find ${pkgdir} -name *.a -delete

  # Install Runtime Library Exception
  install -Dm644 ${srcdir}/gcc-${pkgver}/COPYING.RUNTIME \
    ${pkgdir}/usr/share/licenses/gcc-libs/RUNTIME.LIBRARY.EXCEPTION
}

package_gcc()
{
  pkgdesc="The GNU Compiler Collection - C and C++ frontends"
  depends=("gcc-libs=$pkgver-$pkgrel" 'binutils>=2.23' 'mpc' 'cloog' 'msys2-runtime-devel' 'msys2-w32api-headers' 'msys2-w32api-runtime' 'windows-default-manifest')
  options=('staticlibs' "$STRIP_OPTION" "$DEBUG_OPTION")
  install=gcc.install

  cd ${srcdir}/build-$(arch)

  if check_option "strip" "n"; then
    export STRIP=true
  fi

  make -j1 DESTDIR=${pkgdir} install
  if [ "$CARCH" = "x86_64" ]
  then
    mv $pkgdir/usr/lib/gcc/${CHOST}/lib/libgcc_s.dll.a $pkgdir/usr/lib/gcc/${CHOST}/${pkgver/-*/.*}/
    rm -rf $pkgdir/usr/lib/gcc/${CHOST}/lib
  fi
  # install -d $pkgdir/usr/share/gdb/auto-load/usr/lib
  # mv $pkgdir{,/usr/share/gdb/auto-load}/usr/lib/libstdc++.so.6.0.18-gdb.py

  # unfortunately it is much, much easier to install the lot and clean-up the mess...
  rm $pkgdir/usr/bin/{$CHOST-,}gfortran
  # remove all DLLs
  rm -f $pkgdir/usr/bin/*.dll

  rm -f $pkgdir/usr/lib/libiberty.a
  # rm -f $pkgdir/usr/lib/gcc/${CHOST}/${pkgver}/libatomic{.dll,}.a
  rm -f $pkgdir/usr/lib/gcc/${CHOST}/${pkgver}/libgfortran{.dll,}.a
  rm -f $pkgdir/usr/lib/gcc/${CHOST}/${pkgver}/libgfortran.spec
  rm -r $pkgdir/usr/lib/gcc/$CHOST/${pkgver}/finclude
  rm -f $pkgdir/usr/lib/gcc/$CHOST/${pkgver}/f951.exe
  rm -f $pkgdir/usr/lib/gcc/$CHOST/${pkgver}/{libcaf_single,libgfortranbegin}.a
  rm -f $pkgdir/usr/share/info/{gfortran,libgomp,libquadmath}.info
  rm -f $pkgdir/usr/share/locale/{de,fr}/LC_MESSAGES/libstdc++.mo
  rm -f $pkgdir/usr/share/man/man1/gfortran.1

  # remove static libraries - note libstdc++.a is needed for the binutils and glibc testsuite
  #rm $pkgdir/usr/lib/gcc/${CHOST}/${pkgver}/lib{gomp,quadmath}{.dll,}.a

  # many packages expect this symlinks
  ln -s gcc ${pkgdir}/usr/bin/cc
  cp -f ${pkgdir}/usr/bin/cpp.exe ${pkgdir}/usr/lib/cpp

  # POSIX conformance launcher scripts for c89 and c99
  cat > $pkgdir/usr/bin/c89 <<"EOF"
#!/bin/sh
fl="-std=c89"
for opt; do
  case "$opt" in
    -ansi|-std=c89|-std=iso9899:1990) fl="";;
    -std=*) echo "`basename $0` called with non ANSI/ISO C option $opt" >&2
      exit 1;;
  esac
done
exec gcc $fl ${1+"$@"}
EOF

  cat > $pkgdir/usr/bin/c99 <<"EOF"
#!/bin/sh
fl="-std=c99"
for opt; do
  case "$opt" in
    -std=c99|-std=iso9899:1999) fl="";;
    -std=*) echo "`basename $0` called with non ISO C99 option $opt" >&2
      exit 1;;
  esac
done
exec gcc $fl ${1+"$@"}
EOF

  chmod 755 $pkgdir/usr/bin/c{8,9}9

  # install the libstdc++ man pages
  # install -dm755 ${pkgdir}/usr/share/man/man3
  # install -m644 -t ${pkgdir}/usr/share/man/man3 \
  #  ${CHOST}/libstdc++-v3/doc/doxygen/man/man3/*.3

  # Install Runtime Library Exception
  install -Dm644 ${srcdir}/gcc-${pkgver}/COPYING.RUNTIME \
    ${pkgdir}/usr/share/licenses/gcc/RUNTIME.LIBRARY.EXCEPTION
}

package_gcc-fortran()
{
  pkgdesc="Fortran front-end for GCC"
  depends=("gcc=$pkgver-$pkgrel")
  options=('staticlibs' '!emptydirs' "$STRIP_OPTION" "$DEBUG_OPTION")
  install=gcc-fortran.install

  cd ${srcdir}/build-$(arch)

  if check_option "strip" "n"; then
    export STRIP=true
  fi

  make -j1 DESTDIR=$pkgdir install-target-libgfortran
  make -j1 -C $CHOST/libgomp DESTDIR=$pkgdir install-nodist_fincludeHEADERS
  make -j1 -C gcc DESTDIR=$pkgdir fortran.install-{common,man,info}
  install -Dm755 gcc/f951 $pkgdir/usr/lib/gcc/$CHOST/$pkgver/f951

  ln -s gfortran ${pkgdir}/usr/bin/f95

  rm -f $pkgdir/usr/lib/gcc/${CHOST}/lib/libgcc_s.dll.a
  # remove files included in gcc-libs or gcc and unnneeded static lib
  rm ${pkgdir}/usr/bin/*.dll
  rm ${pkgdir}/usr/lib/gcc/$CHOST/${pkgver}/libquadmath.{dll.a,a}
  rm ${pkgdir}/usr/lib/gcc/$CHOST/${pkgver}/{*.o,libgc*}
  rm ${pkgdir}/usr/share/info/libquadmath.info
  rm -r ${pkgdir}/usr/lib/gcc/$CHOST/${pkgver}/include
  # rm ${pkgdir}/usr/lib/gcc/$CHOST/${pkgver}/libgfortran.a

  # Install Runtime Library Exception
  install -Dm644 ${srcdir}/gcc-${pkgver}/COPYING.RUNTIME \
    ${pkgdir}/usr/share/licenses/gcc-fortran/RUNTIME.LIBRARY.EXCEPTION
}
